package com.mdinic.blogovi.front.page.person;

import java.util.Date;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.mdinic.blogovi.front.common.BDateLabel;
import com.mdinic.blogovi.front.common.PersonPhoto;
import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.front.page.person.picture.UploadPhotoPage;
import com.mdinic.blogovi.model.BlogPost;
import com.mdinic.blogovi.model.Comment;
import com.mdinic.blogovi.model.Fav;
import com.mdinic.blogovi.model.Person;
import com.mdinic.blogovi.model.Vote;
import com.mdinic.blogovi.service.PersonService;

public class PersonViewPanel extends Panel {

    private static final long serialVersionUID = 1L;

    @SpringBean
    private transient PersonService personService;

    public PersonViewPanel(String id, final IModel<Person> personModel) {
        super(id, personModel);

        BookmarkablePageLink<UploadPhotoPage> uploadPhotoLink = new BookmarkablePageLink<UploadPhotoPage>(
                "upload_link", UploadPhotoPage.class);
        uploadPhotoLink.setVisible(BSession.get().isLoggedIn());

        BookmarkablePageLink<PersonEditPage> personEditLink = new BookmarkablePageLink<PersonEditPage>(
                "person_edit_link", PersonEditPage.class);
        personEditLink.setVisible(BSession.get().isLoggedIn());

        boolean me = BSession.get().isLoggedIn()
                && BSession.get().getPerson().getId().equals(personModel.getObject().getId());
        add(personEditLink.setVisible(me));
        add(uploadPhotoLink.setVisible(me));

        add(new PersonPhoto("photo", personModel.getObject()));

        add(new Label(Person.NICKNAME, new PropertyModel<String>(personModel, Person.NICKNAME)));
        add(new Label(Person.FIRSTNAME, new PropertyModel<String>(personModel, Person.FIRSTNAME)));
        add(new Label(Person.LASTNAME, new PropertyModel<String>(personModel, Person.LASTNAME)));
        add(new BDateLabel(Person.JOINED, new PropertyModel<Date>(personModel, Person.JOINED)));

        ExternalLink blogLink = new ExternalLink(Person.BLOG, new PropertyModel<String>(personModel, Person.BLOG));
        blogLink.setVisible(personModel.getObject().getBlog() != null);
        blogLink.add(new Label("blog_url", new PropertyModel<String>(personModel, Person.BLOG)));
        add(blogLink);

        ExternalLink websiteLink = new ExternalLink(Person.WEBSITE, new PropertyModel<String>(personModel,
                Person.WEBSITE));
        websiteLink.setVisible(personModel.getObject().getWebsite() != null);
        websiteLink.add(new Label("website_url", new PropertyModel<String>(personModel, Person.WEBSITE)));
        add(websiteLink);

        add(new Label("post_count", new Model<Long>(BlogPost.countBlogPostsByPerson(personModel.getObject()))));

        add(new Label("comment_count", new Model<Long>(Comment.countCommentsByPerson(personModel.getObject()))));

        add(new Label("up_votes_received_count",
                new Model<Long>(BlogPost.countVotesUpByPerson(personModel.getObject()))));
        add(new Label("down_votes_received_count", new Model<Long>(BlogPost.countVotesDownByPerson(personModel
                .getObject()))));

        add(new Label("up_votes_count", new Model<Long>(Vote.countVotesUpByPerson(personModel.getObject()))));
        add(new Label("down_votes_count", new Model<Long>(Vote.countVotesDownByPerson(personModel.getObject()))));

        add(new Label("favs_count", new Model<Long>(Fav.countFavsByPerson(personModel.getObject()))));

        boolean follow = false;
        if (BSession.get().isLoggedIn()) {
            follow = personService.doIFollow(BSession.get().getPerson(), personModel.getObject());
        }

        final Label image = new Label("follow_img", follow ? "Otprati" : "Prati");
        image.setOutputMarkupId(true);
        AjaxLink<Void> followLink = new AjaxLink<Void>("follow_link") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                boolean follow = personService.follow(BSession.get().getPerson(), personModel.getObject());
                if (follow) {
                    image.setDefaultModelObject("Otprati");
                } else {
                    image.setDefaultModelObject("Prati");
                }
                target.add(image);
            }

        };
        followLink.setVisible(BSession.get().isLoggedIn()
                && !BSession.get().getPerson().getId().equals(personModel.getObject().getId()));

        followLink.add(image);
        add(followLink);

    }
}
